const moment = require('moment');
const jwt = require('jsonwebtoken');
const utilTool = require('../../util');
const $config = require('../../config');

module.exports = {
    loginRegister: async (ctx, next) => {
        $config.documents = 'site' // 切换文档集合
        // 获取结构注册参数
        const {userName, passWord, email} = ctx.request.body;
        const verifyUserName = await utilTool.find({userName: userName},{_id:0,userName:1});
        let isConflict = false;
        if (verifyUserName && verifyUserName.length>0){
            if (verifyUserName[0].userName === userName) {
                ctx.body = {
                    success:false,
                    result:'用户名已注册！'
                };
                isConflict = true;
            }
        }
        if (isConflict) return; // 用户名已注册进行阻断
        // 查询条件
        const insrtData = {
            userName,
            passWord: await utilTool.md5(passWord),
            email,
        };
        if (userName) {
            const result = await utilTool.insertOne(insrtData);
            if (result.result.ok) {
                ctx.body = {
                    success: true,
                    result: '注册成功！'
                };
            }
        }
    },
    loginCheck: async(ctx, next) => {
        $config.documents = 'site' // 切换文档集合
        // 获取解构登录参数
        const {userName, passWord} = ctx.request.body;
        const md5Password = await utilTool.md5(passWord);
        const verifyPassword = await utilTool.find({userName: userName},{_id:1,passWord:1,access:1});
        const ArrayPassword = [passWord, md5Password];
        if (!verifyPassword.length) {
            ctx.body = {
                success: false,
                result:'用户名不正确！'
            };
            return false;
        }
        if (!ArrayPassword.includes(verifyPassword[0].passWord)) {
            ctx.body = {
                success: false,
                result:'密码错误！'
            };
            return false;
        }
        if (userName) {
            const token = await jwt.sign({ userName: userName }, $config.tokenSecret, {expiresIn: '24h'});  //token加密 有效期为1小时
            ctx.body = {
                success: true,
                token,
                id: verifyPassword[0]._id,
                access: verifyPassword[0].access,
                authRoute: [
                    {
                        path: '/home',
                        name: 'home',
                        component: 'Layout',
                        meta: {
                            title: '首页',
                            noCache: true,
                            hideInMenu: true,
                        },
                        children: [
                            {
                                path: 'homePage',
                                name: 'homePage',
                                component: 'a-home/HomePage.vue',
                                meta: {
                                    title: '首页展示',
                                    noCache: true,
                                    access: ['admin'],
                                },
                            },
                        ],
                    },
                    {
                        path: '/account',
                        name: 'account',
                        component: 'Layout',
                        meta: {
                            title: '用户管理',
                            icon: 'icon-drxx10',
                            access: ['admin'],
                        },
                        children: [
                            {
                                path: 'permission',
                                name: 'permission',
                                component: 'account/permission/Permission.vue',
                                meta: {
                                    title: '用户权限',
                                    noCache: true,
                                    access: ['superAdmin'],
                                },
                            },
                            {
                                path: 'optionTwo',
                                name: 'optionTwo',
                                component: 'account/optionTwo/OptionTwo.vue',
                                meta: {
                                    title: '选项二',
                                    noCache: false,
                                    access: ['admin'],
                                },
                            },
                        ],
                    },
                    {
                        path: '/components',
                        name: 'components',
                        meta: {
                            title: '组件',
                            icon: 'icon-tongji',
                            access: ['components'],
                        },
                        component: 'Layout',
                        children: [
                            {
                                path: 'imgZip',
                                name: 'imgZip',
                                meta: {
                                    title: '照片压缩',
                                    access: ['admin'],
                                    href: 'https://my.oschina.net/yangyishe/blog/3055732',
                                },
                                component: 'components/imgZip/index.vue',
                            },
                            {
                                path: 'drag_list_page',
                                name: 'drag_list_page',
                                meta: {
                                    title: '拖拽列表',
                                    access: ['admin'],
                                },
                                component: 'components/drag-list/index.vue'
                            },
                        ],
                    },
                ],
            };
        }
    }
};
